package com.nativetalk.controller.app.feedback;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.feedback.TdNtFeedbackInsert;
import com.nativetalk.bean.feedback.TdNtFeedbackTeaInsert;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.feedback.insert.FeedbackInsertService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class FeedbackController {

    private static Logger logger = Logger.getLogger(FeedbackController.class);

    @Resource
    private FeedbackInsertService feedbackInsertService;

    /**
     * @return RetInfo
     * @Purpose 添加意见反馈
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/feedback/add", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo member(@Valid @RequestBody TdNtFeedbackInsert tdNtFeedbackInsert, BindingResult result,
                          @RequestHeader(value = "token", required = true) String token) {

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtFeedbackInsert));

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            if (result.hasErrors()) {
                RetInfo retInfo = new RetInfo();
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            tdNtFeedbackInsert.setUser_id(tdNtMember.getMemb_id());
            tdNtFeedbackInsert.setUser_cate("1");
            tdNtFeedbackInsert.setParent_id(0);
            RetInfo retInfo = feedbackInsertService.addFeedback(tdNtFeedbackInsert);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 教师添加意见反馈
     * @version 2.1
     * @time 2016-05-23
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/feedback/reply", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo reply(@Valid @RequestBody TdNtFeedbackTeaInsert tdNtFeedbackTeaInsert, BindingResult result,
                         @RequestHeader(value = "token", required = true) String token) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            TdNtFeedbackInsert tdNtFeedbackInsert = new TdNtFeedbackInsert();
            tdNtFeedbackInsert.setFeed_content(tdNtFeedbackTeaInsert.getFeed_content());
            tdNtFeedbackInsert.setUser_id(tdNtTeacherResult.getTea_id());
            tdNtFeedbackInsert.setUser_cate("2");
            retInfo = feedbackInsertService.addFeedback(tdNtFeedbackInsert);
        }
        return retInfo;
    }

}


